package com.bookstore.fliter;

import com.bookstore.pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter",urlPatterns = {"/pages/manager/*"})
public class LoginFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req= (HttpServletRequest) request;
        HttpServletResponse resp= (HttpServletResponse) response;
        resp.setHeader("Content-type","text/html;charset=utf-8");
        HttpSession session = req.getSession();
        User user=(User)session.getAttribute("user");
        if(user!=null){
            chain.doFilter(request, response);
        }
        String msg ="请登录!!!!";
        req.setAttribute("msg",msg);
        req.getRequestDispatcher("pages/user/login.html").forward(request,response);
    }
}
